//
//  OrderView.m
//  GuoBang
//
//  Created by 梧桐树 on 16/5/3.
//  Copyright © 2016年 梧桐树. All rights reserved.
//

#import "OrderView.h"

@implementation OrderView
-(instancetype)initWithFrame:(CGRect)frame{
  self=[super initWithFrame:frame];
    if (self) {
      

        
    }
    return self;
}
-(void)loadView{
    scr=[UIScrollView new];
    scr.delegate=self;
    scr.pagingEnabled=YES;
    scr.showsHorizontalScrollIndicator=NO;
    scr.showsVerticalScrollIndicator=NO;
    int orX=0;
    int orY=0;
    
    for (int i=0; i<_dataArry.count; i++) {
        switch (_style) {
            case Huafei:
            {
                HuaFeiView *cellView=[[HuaFeiView alloc]initWithFrame:CGRectMake(orX, orY, self.frame.size.width, 50) withtitle:@"电信话费" money:@"50元" time:@"十分钟到账" chongzhijuan:@"无可用充值券" btnImage:[UIImage imageNamed:@"order_ico_pay@2x.png"]];
                orY+=cellView.frame.size.height;
                
                if (orY/3==cellView.frame.size.height) {
                    
                    orX+=cellView.frame.size.width;
                    orY=0;
                }
                
                [scr addSubview:cellView];
                if (i==_dataArry.count-1) {
                    if (orY==0) {
                        orX-=cellView.frame.size.width;
                    }
                    scr.frame=CGRectMake(0, line.frame.size.height+line.frame.origin.y, self.frame.size.width, cellView.frame.size.height*3);
                    scr.contentSize=CGSizeMake(orX+cellView.frame.size.width, orY+cellView.frame.size.height);
                }
                
                
            }
                break;
            case Kafei :
            {
                HuaFeiView *cellView=[[HuaFeiView alloc]initWithFrame:CGRectMake(orX, orY, self.frame.size.width, 50) withtitle:@"卡布奇诺" money:@"" time:@"15分钟送达" chongzhijuan:@"" btnImage:[UIImage imageNamed:@"order_ico_ding.png"]];
                
                [cellView.chongBtn addTarget:self action:@selector(kafei:) forControlEvents:UIControlEventTouchUpInside];
                
                orY+=cellView.frame.size.height;
                
                if (orY/3==cellView.frame.size.height) {
                    
                    orX+=cellView.frame.size.width;
                    orY=0;
                }
                
                [scr addSubview:cellView];
                if (i==_dataArry.count-1) {
                    if (orY==0) {
                        orX-=cellView.frame.size.width;
                    }
                    scr.frame=CGRectMake(0, line.frame.size.height+line.frame.origin.y, self.frame.size.width, cellView.frame.size.height*3);
                    scr.contentSize=CGSizeMake(orX+cellView.frame.size.width, orY+cellView.frame.size.height);
                }
                
                
            }
                break;
            case Dache :
            {
                if(i==0){
                HuaFeiView *cellView=[[HuaFeiView alloc]initWithFrame:CGRectMake(orX, orY, self.frame.size.width, 50) withtitle:@"您还没有打车记录" money:@"" time:@"点击上方[+],添加地址吧" chongzhijuan:@"" btnImage:[UIImage imageNamed:@""]];
                    
                   
                    
                orY+=cellView.frame.size.height;
                
                if (orY/3==cellView.frame.size.height) {
                    
                    orX+=cellView.frame.size.width;
                    orY=0;
                }
                
                [scr addSubview:cellView];
                    [self.addBtn addTarget:self action:@selector(dache:) forControlEvents:UIControlEventTouchUpInside];
                    if (orY==0) {
                        orX-=cellView.frame.size.width;
                    }
                    scr.frame=CGRectMake(0, line.frame.size.height+line.frame.origin.y, self.frame.size.width, cellView.frame.size.height*3);
                    scr.contentSize=CGSizeMake(orX+cellView.frame.size.width, orY+cellView.frame.size.height);
              //  }
               
                
                }
            }
                break;
            case Xiawucha :
            {
                DanGaoView *cellView=[[DanGaoView alloc]initWithFrame:CGRectMake(orX, orY, self.frame.size.width, 50) withImage:[UIImage imageNamed:@"img_ship.png"] title:@"可爱美味小蛋糕" detailsTitle:@"美味小蛋糕" peiliao:@"奶油，蛋糕" price:@"123元/个"];
                orY+=cellView.frame.size.height;
                
                if (orY/3==cellView.frame.size.height) {
                    
                    orX+=cellView.frame.size.width;
                    orY=0;
                }
                
                [scr addSubview:cellView];
                if (i==_dataArry.count-1) {
                    if (orY==0) {
                        orX-=cellView.frame.size.width;
                    }
                    scr.frame=CGRectMake(0, line.frame.size.height+line.frame.origin.y, self.frame.size.width, cellView.frame.size.height*3);
                    scr.contentSize=CGSizeMake(orX+cellView.frame.size.width, orY+cellView.frame.size.height);
                }
                
                
            }
                break;
            case Lingshi :
            {
                DanGaoView *cellView=[[DanGaoView alloc]initWithFrame:CGRectMake(orX, orY, self.frame.size.width, 50) withImage:[UIImage imageNamed:@"img_ship.png"] title:@"瓜子" detailsTitle:@"店家亲自炒的瓜子" peiliao:@"花椒，大料，生抽，香油" price:@"12元/包"];
                orY+=cellView.frame.size.height;
                
                if (orY/3==cellView.frame.size.height) {
                    
                    orX+=cellView.frame.size.width;
                    orY=0;
                }
                
                [scr addSubview:cellView];
                if (i==_dataArry.count-1) {
                    if (orY==0) {
                        orX-=cellView.frame.size.width;
                    }
                    scr.frame=CGRectMake(0, line.frame.size.height+line.frame.origin.y, self.frame.size.width, cellView.frame.size.height*3);
                    scr.contentSize=CGSizeMake(orX+cellView.frame.size.width, orY+cellView.frame.size.height);
                }
                
                
            }
                break;
            case Shuiguo :
            {
                DanGaoView *cellView=[[DanGaoView alloc]initWithFrame:CGRectMake(orX, orY, self.frame.size.width, 50) withImage:[UIImage imageNamed:@"img_ship.png"] title:@"苹果" detailsTitle:@"又香又甜又可口" peiliao:@"苹果树" price:@"15元/斤"];
                orY+=cellView.frame.size.height;
                
                if (orY/3==cellView.frame.size.height) {
                    
                    orX+=cellView.frame.size.width;
                    orY=0;
                }
                
                [scr addSubview:cellView];
                if (i==_dataArry.count-1) {
                    if (orY==0) {
                        orX-=cellView.frame.size.width;
                    }
                    scr.frame=CGRectMake(0, line.frame.size.height+line.frame.origin.y, self.frame.size.width, cellView.frame.size.height*3);
                    scr.contentSize=CGSizeMake(orX+cellView.frame.size.width, orY+cellView.frame.size.height);
                }
                
                
            }
                break;
            case Gengduo :
            {
                
                MovieView *cellView=[[MovieView alloc]initWithFrame:CGRectMake(orX, orY, self.frame.size.width, 50) withImage:[UIImage imageNamed:@"ico_movie_1@2x.png"] title:@"电影" detailsTitle:@"订电影票" buttonImage:[UIImage imageNamed:@"order_ico_ding.png"]];
                orY+=cellView.frame.size.height;
                
                if (orY/3==cellView.frame.size.height) {
                    
                    orX+=cellView.frame.size.width;
                    orY=0;
                }
                
                [scr addSubview:cellView];
                if (i==_dataArry.count-1) {
                    if (orY==0) {
                        orX-=cellView.frame.size.width;
                    }
                    scr.frame=CGRectMake(0, line.frame.size.height+line.frame.origin.y, self.frame.size.width, cellView.frame.size.height*3);
                    scr.contentSize=CGSizeMake(orX+cellView.frame.size.width, orY+cellView.frame.size.height);
                }
            }
                break;
                
            default:
                break;
        }
        
    }
    
    [self addSubview:scr];
    //=======pagecontrol
    pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scr.frame.size.height+scr.frame.origin.y+5, self.frame.size.width, 30)];  //创建UIPageControl，位置在屏幕最下方。
    pageCtrl.numberOfPages = scr.contentSize.width/scr.frame.size.width;//总的图片页数
    pageCtrl.currentPage = 0; //当前页
    pageCtrl.selected=NO;
    pageCtrl.currentPageIndicatorTintColor=[SEATools stringToColor:@"ff6d6d"];
    pageCtrl.pageIndicatorTintColor=[SEATools stringToColor:@"efeff4"];
    
    [self addSubview:pageCtrl];  //将UIPageControl添加到主界面上。
    
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, pageCtrl.frame.origin.y+pageCtrl.frame.size.height);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //更新UIPageControl的当前页
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = self.frame;
    [pageCtrl setCurrentPage:offset.x / bounds.size.width];
    
}


- (void)kafei:(UIButton *)sender
{
    NSLog(@"到底点击了吗");
    
    [self.delegate dingKaFei];
}


- (void)dache:(UIButton *)sender
{
      NSLog(@"车走了吗");
    [self.delegate yaodache];
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
